//
//  HMTabBar.swift
//  sina
//
//  Created by Kent on 16/9/22.
//  Copyright © 2016年 Ming. All rights reserved.
//

import UIKit

class HMTabBar: UITabBar {
    
    var composeClosure: (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeBtn)
        composeBtn.addTarget(self, action: #selector(composeBtnClick), for: .touchUpInside)
        
    }
    
    @objc private func composeBtnClick() {
        composeClosure?()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        let btnW = UIScreen.main.bounds.size.width / 5
        let btnH = self.bounds.size.height
        
        var index = 0
        for subView in subviews {
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subView.frame = CGRect(x: CGFloat(index) * btnW, y: 0, width: btnW, height: btnH)
                
                index += (index == 1) ? 2 : 1
            }
        }
        
        composeBtn.bounds.size = CGSize(width: btnW, height: btnH)
        composeBtn.center = CGPoint(x: self.center.x, y: self.bounds.height * 0.5)
    }
    
    lazy var composeBtn: UIButton = {
        let btnW = UIScreen.main.bounds.size.width / 5
        let btnH = self.bounds.size.height
        let btn = UIButton()
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        return btn
    }()
    
}
